//配置GiteeID
var giteeID = 'DSR-Inc_0';


//颜色配置
var LightBG = 'rgb(255,250,245) !important';
var DarkBG = 'rgb(40,45,50) !important';

var LightFont = '#666 !important';
var DarkFont = '#DDD !important';

var LightOther = 'rgb(255,165,85)';
var DarkOther = 'rgb(85,165,255)';

var LightFrost = 'rgba(255,250,245,0.5)';
var DarkFrost = 'rgba(100,105,110,0.5)';

var LightBodyBG = 'rgb(235,230,225)';
var DarkBodyBG = 'rgb(40,45,50)';

//var LightRange = 'rgb(255,165,85)';
//var DarkRange = 'rgb(85,165,255)';

var time = 600;

const utf8to16 = (str) => {
    var out, i, len, c;
    var char2, char3;
    out = "";
    len = str.length;
    i = 0;
    while (i < len) {
        c = str.charCodeAt(i++);
        switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                out += str.charAt(i - 1);
                break;
            case 12: case 13:
                char2 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                char2 = str.charCodeAt(i++);
                char3 = str.charCodeAt(i++);
                out += String.fromCharCode(((c & 0x0F) << 12) |
                    ((char2 & 0x3F) << 6) |
                    ((char3 & 0x3F) << 0));
                break;
        }
    }
    return out;
}

//请求头像与个人信息
function myinfo() {
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/myinfo',
        success: function (result) {
            if ((result != 404)) {
                $(this).attr('src', data.imgsrc)
            } else {
                alert("加载出错，请检查网络状况");
            }
        }
    })
}

//引导页夜间模式平滑过渡
function indexbgcolor() {
    $('#darkMode').hasClass('Light') ?
        [
            $('body').stop().animate({ backgroundColor: DarkBodyBG }, time),
            $('a,p').stop().animate({ color: DarkFont }, time),
            $('#cardbox').stop().animate({ backgroundColor: DarkFrost }, time),
            $('a#ToBlogBtn').stop().animate({ backgroundColor: DarkOther }, time)
        ] :
        [
            $('body').stop().animate({ backgroundColor: LightBodyBG }, time),
            $('a,p').stop().animate({ color: LightFont }, time),
            $('#cardbox').stop().animate({ backgroundColor: LightFrost }, time),
            $('a#ToBlogBtn').stop().animate({ backgroundColor: LightOther }, time)
        ];
}


//通用夜间模式平滑过渡
function commonbgcolor() {
    $('#darkMode').hasClass('Light') ?
        [
            $('body').stop().animate({ backgroundColor: DarkBodyBG }, time),
            $('i,a,p,.notetag').stop().animate({ color: DarkFont }, time),
            $('.ModeBG').stop().animate({ backgroundColor: DarkFrost }, time),
            $('#toggle,.ops-in,.notetag').stop().animate({ backgroundColor: DarkOther }, time),
            $('input[type=range]').stop().animate({ backgroundColor: DarkOther }, time),
        ] :
        [
            $('body').stop().animate({ backgroundColor: LightBodyBG }, time),
            $('i,a,p,.notetag').stop().animate({ color: LightFont }, time),
            $('.ModeBG').stop().animate({ backgroundColor: LightFrost }, time),
            $('#toggle,.ops-in,.notetag').stop().animate({ backgroundColor: LightOther }, time),
            $('input[type=range]').stop().animate({ backgroundColor: LightOther }, time),
        ];
}

//首页夜间模式平滑过渡
function blogbgcolor() {
    //移入ModeSwitch.js
}

//动态夜间模式平滑过渡
function dynamicbgcolor() {
    $('#darkMode').hasClass('Light') ?
        [
            $('.dynamic').stop().animate({ backgroundColor: DarkFrost }, time),
        ] :
        [
            $('.dynamic').stop().animate({ backgroundColor: LightFrost }, time),
        ];
}
//笔记夜间模式平滑过渡
function markbgcolor() {
    $('#darkMode').hasClass('Light') ?
        [
            $('pre.line-numbers').stop().animate({ backgroundColor: DarkBG }, time),
            $('.notename').stop().animate({ color: '#FFF' }, time),
            $('.toread').stop().animate({ color: DarkOther }, time),
        ] :
        [
            $('pre.line-numbers').stop().animate({ backgroundColor: LightBG }, time),
            $('.notename').stop().animate({ color: '#333' }, time),
            $('.toread').stop().animate({ color: LightOther }, time),
        ];
}

//友链夜间模式平滑过渡
function friendbgcolor() {
    $('#darkMode').hasClass('Light') ?
        [
            $('.friendBtn').find('a').stop().animate({ backgroundColor: DarkOther }, time),

        ] :
        [
            $('.friendBtn').find('a').stop().animate({ backgroundColor: LightOther }, time),
        ];
}

//自适应高度(iframe加载完毕后调用)
function iframeOn(page) {
    var iframeObj = document.getElementById('container');
    var optionsObj = parent.document.getElementById('options');
    var sideboxObj = parent.document.getElementById('sidebox');
    setTimeout(() => {
        iframeObj.scrollHeight <= sideboxObj.scrollHeight ?
            $('#' + page + '_iframe', window.parent.document).css('height', sideboxObj.scrollHeight + optionsObj.scrollHeight) :
            $('#' + page + '_iframe', window.parent.document).css('height', iframeObj.scrollHeight + optionsObj.scrollHeight);
    })
}

//夜间模式切换
$('.modeSwitch').click(function () {
    ModeSwitch.modeSwitch();
    commonbgcolor();
})

//切换iframe
var iframe;
function iframeload() {
    var opslength = new Array();
    var iframeList = '';
    document.getElementsByClassName('math');
    for (var i = 0; i < document.getElementsByClassName('math').length; i++) {
        opslength.push(document.getElementsByClassName('math')[i].id);
    }
    opslength.forEach(function (item) {
        iframeList += `
            <iframe src="/iframe/${item}.html" id="${item}_iframe" class="iframe" frameborder="0" scrolling="no" allowtransparency="true"></iframe>
        `
    })

    $('#centerbox').prepend(iframeList);

    (window.location.hash) ? [
        $('#' + window.location.hash.substring(window.location.hash.search('#') + 1, window.location.hash.length) + '_iframe').show(),
        (window.location.hash == '#notes') ? $('#notestag').show() : $('#notestag').hide(),
    ] : [
        $('#homepage_iframe').show(),
        window.location.hash = 'homepage',
    ];

    $('#' + window.location.hash.substring(window.location.hash.search('#') + 1, window.location.hash.length)).find('div').addClass('ops-in');
}

function pageclick(thisid) {
    window.location.hash = thisid.id.toString();
    $('#' + thisid.id.toString() + '_iframe').show().siblings('iframe').hide();
    $(thisid).find('div').addClass('ops-in').parent('a').siblings().find('div').removeClass('ops-in').css('background-color', '');
    (window.location.hash == '#notes') ? $('#notestag').show() : $('#notestag').hide();
}